package graduation.dao.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import graduation.dao.SuggestionDao;
import graduation.entity.Suggestion;
import graduation.vo.PageVO;

@Repository
public class SuggestionDaoImpl extends HibernateDaoSupport implements SuggestionDao{
	@Autowired  
    public void setSessionFactoryOverride(SessionFactory sessionFactory)  
    {  
        super.setSessionFactory(sessionFactory);  
    }
	
	@Resource
	private SessionFactory sessionFactory;

	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	/**
	 * 添加到数据库
	 */
	@Override
	public void addSuggestion(Suggestion suggestion) {
		// TODO Auto-generated method stub
		this.getSession().save(suggestion);
	}
	/**
	 * 查询全部意见
	 */
	public PageVO getAllSuggestion(int page,int size){
		
		Session session = this.getSession();
		Query query = session.createQuery("from Suggestion");
		//得到滚动结果集
		ScrollableResults scroll = query.scroll();
		//滚动到最后一行
		scroll.last();
		
		int count = scroll.getRowNumber() + 1;//总行数
		//设置分页信息
		query.setFirstResult(page - 1);
		query.setMaxResults(size);
		PageVO pageMessage = new PageVO();
		pageMessage.setCountPage((count/size) + 1);
		pageMessage.setList(query.list());
		pageMessage.setRecordsNum(count);
		return pageMessage;
		
	}
}
